package com.dlc.shop.delivery.multishop.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dlc.shop.common.exception.YamiShopBindException;
import com.dlc.shop.common.response.ServerResponseEntity;
import com.dlc.shop.common.util.PageParam;
import com.dlc.shop.delivery.common.model.Printer;
import com.dlc.shop.delivery.common.service.PrinterService;
import com.dlc.shop.security.multishop.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;


/**
 * 打印机
 * @author TRACK
 */
@RestController
@RequestMapping("/shop/printer")
@Tag(name = "打印机")
@AllArgsConstructor
public class PrinterController {

    private final PrinterService printerService;

    @GetMapping("/page")
    @Operation(summary = "获取打印机列表", description = "分页获取打印机列表")
    @PreAuthorize("@pms.hasPermission('delivery:printer:page')")
    public ServerResponseEntity<IPage<Printer>> getPrinterPage(PageParam<Printer> page, Printer printer) {
        return ServerResponseEntity.success(printerService.page(page, new LambdaQueryWrapper<Printer>()
                .like(StrUtil.isNotBlank(printer.getPrinterName()), Printer::getPrinterName, printer.getPrinterName())
                .eq(Printer::getShopId, SecurityUtils.getShopUser().getShopId())));
    }

    @GetMapping("/info/{printerId}")
    @Operation(summary = "获取打印机", description = "根据printerId获取打印机")
    @Parameter(name = "printerId", description = "打印机", required = true)
    @PreAuthorize("@pms.hasPermission('delivery:printer:get')")
    public ServerResponseEntity<Printer> getById(@PathVariable("printerId") Long printerId) {
        Printer printer = printerService.getById(printerId);
        checkAuth(printer);
        return ServerResponseEntity.success(printer);
    }

    private static void checkAuth(Printer printer) {
        if (Objects.isNull(printer)) {
            throw new YamiShopBindException("yami.print.is.not.exist");
        }
        if (!Objects.equals(printer.getShopId(), SecurityUtils.getShopUser().getShopId())) {
            throw new YamiShopBindException("yami.operate.auth");
        }
    }

    @PostMapping
    @PreAuthorize("@pms.hasPermission('delivery:printer:save')")
    @Operation(summary = "保存打印机", description = "保存打印机")
    public ServerResponseEntity<Boolean> save(@RequestBody @Valid Printer printer) {
        printer.setShopId(SecurityUtils.getShopUser().getShopId());
        printer.setPrinterId(null);
        printerService.insert(printer);
        printerService.removeCache(SecurityUtils.getShopUser().getShopId());
        return ServerResponseEntity.success();
    }

    @PutMapping
    @PreAuthorize("@pms.hasPermission('delivery:printer:update')")
    @Operation(summary = "更新打印机", description = "更新打印机")
    public ServerResponseEntity<Boolean> updateById(@RequestBody @Valid Printer printer) {
        printer.setShopId(SecurityUtils.getShopUser().getShopId());
        printerService.update(printer);
        printerService.removeCache(SecurityUtils.getShopUser().getShopId());
        return ServerResponseEntity.success();
    }

    @DeleteMapping("/{printerId}")
    @PreAuthorize("@pms.hasPermission('delivery:printer:delete')")
    @Operation(summary = "删除打印机", description = "根据打印机id删除打印机")
    @Parameter(name = "printerId", description = "打印机", required = true)
    public ServerResponseEntity<Boolean> removeById(@PathVariable Long printerId) {
        Printer printer = printerService.getById(printerId);
        checkAuth(printer);
        printerService.removeById(printerId);
        printerService.removeCache(SecurityUtils.getShopUser().getShopId());
        return ServerResponseEntity.success();
    }

    @PutMapping("/setDefault/{printerId}")
    @Operation(summary = "设打印机信息为默认/取消默认")
    @PreAuthorize("@pms.hasPermission('delivery:printer:setDefault')")
    public ServerResponseEntity<Void> setDefault(@PathVariable Long printerId) {
        Printer printer = printerService.getById(printerId);
        checkAuth(printer);
        printerService.setDefault(printerId);
        printerService.removeCache(SecurityUtils.getShopUser().getShopId());
        return ServerResponseEntity.success();
    }

    @GetMapping("/listPrinter")
    @Operation(summary = "获取打印机列表")
    @PreAuthorize("@pms.hasPermission('delivery:printer:list')")
    public ServerResponseEntity<List<Printer>> listPrinter() {
        List<Printer> list = printerService.listByShopId(SecurityUtils.getShopUser().getShopId());
        return ServerResponseEntity.success(list);
    }
}
